package com.example.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.pojo.Category;
import com.example.ruiji.pojo.Employee;
import com.example.ruiji.pojo.R;
import com.example.ruiji.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
@Api(value = "菜品分类接口")
public class CategoryController {
@Autowired
    private CategoryService categoryService;
@PostMapping
@ApiOperation(value = "菜品新增")
@ApiImplicitParams({
        @ApiImplicitParam(name="category",value = "传递的套餐数据",required=true)
})
public R<String> save( @RequestBody Category category)
    {
categoryService.save(category);
return R.success("新增成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize)
    {
        ///配置分页插件 --返回数据库所查询的页码值
        Page Pageinfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaUpdateWrapper<Category> queryWrapper=new LambdaUpdateWrapper();

        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);

        //执行查询
        categoryService.page(Pageinfo,queryWrapper);
        return R.success(Pageinfo);

    }
@DeleteMapping
    public R<String> delet(Long ids)

{
  categoryService.deletA(ids);

    return R.success("修改分类信息成功");
}

    @GetMapping("/list")
    public R<List<Category>> list(Category category)
    {

        //构造条件构造器
        LambdaUpdateWrapper<Category> queryWrapper=new LambdaUpdateWrapper();
         queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //执行查询

        List<Category> list=categoryService.list(queryWrapper);

        return R.success(list);

    }
}
